package com.gexiaobao.pigeon.ui.breeding_pigeon

import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.ext.hideSoftKeyboard
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityBreedingManagementBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterBreedingManage
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.ui.dialog.RxInputCustomDialog
import com.gexiaobao.pigeon.viewmodel.BreedingPigeonViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/9/2 14:13
 * @Describe    : 作育管理
 */
class ActivityBreedingManagement : BaseActivity<BreedingPigeonViewModel, ActivityBreedingManagementBinding>() {

    private val mAdapter: AdapterBreedingManage by lazy { AdapterBreedingManage() }
    private lateinit var loadSir: LoadService<Any>
    private var mPigeonId = 0
    private var intentType = ""//rear-单独作育管理进入，blood-种鸽列表进入
    private var mSex = ""

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)

        mDatabind.includeBar.tvTitle.text = getString(R.string.Breeding_management)

        mDatabind.includeSearch.editQuery.hint = getString(R.string.please_input_name_and_ring)
        intentType = intent.getStringExtra("intentType").toString()
        mSex = intent.getStringExtra("sex").toString()
        mPigeonId = intent.getIntExtra("pigeonId", 0)

        if (intentType == "blood") {
            mDatabind.toolBarSearch.isVisible = false
            mDatabind.includeBar.toolBarLayout.isVisible = true
        } else {
            mDatabind.toolBarSearch.isVisible = true
            mDatabind.includeBar.toolBarLayout.isVisible = false
        }

        loadSir = loadServiceInit(mDatabind.includeList.swipeRefreshLayout) {
            onRefresh(true)
        }

        onRefresh(true)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 12f)))
            it.initFooter {
                onRefresh(false)
            }
        }

        mDatabind.includeSearch.editQuery.setOnEditorActionListener { _, i, _ ->
            when (i) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    onRefresh(true)
                    hideSoftKeyboard(this)
                }

                else -> {}
            }
            false
        }

        mDatabind.includeSearch.editQuery.addTextChangedListener {
            mDatabind.includeSearch.ivClose.isVisible = it.toString().isNotEmpty()
        }

        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh(true)
        }

        mAdapter.addChildClickViewIds(R.id.ivDeletePairInfo, R.id.ivEditPairNum)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.ivEditPairNum -> showEditPairNumDialog(mAdapter.data[position].id, mAdapter.data[position].pairNum)
                R.id.ivDeletePairInfo -> showDeleteDialog(mAdapter.data[position].id)
            }
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            startActivity<ActivityBreedingRecord>("breedingId" to mAdapter.data[position].id)
        }

        eventViewModel.toRefreshPair.observeInActivity(this) {
            onRefresh(true)
        }
    }

    private fun showEditPairNumDialog(id: Int, pairNum: String) {
        val dialog = RxInputCustomDialog(this)
        dialog.setTitle(getString(R.string.input_new_pair_ring))
        dialog.setMaxLength(15)
        dialog.setContent(pairNum)
        dialog.setSureListener {
            val content = dialog.getContent()
            mViewModel.editBreedingPairInfo(id, content)
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    private fun showDeleteDialog(id: Int) {
        val dialog = RxDialogDefault(this)
        dialog.setTitle(getString(R.string.sure_delete_this_data))
        dialog.setSureListener {
            dialog.dismiss()
            mViewModel.delBreedingPairInfo(id.toString())
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    private fun onRefresh(isRefresh: Boolean) {
        val query = mDatabind.includeSearch.editQuery.text.toString().trim()
        if (isRefresh) {
            loadSir.showLoading()
        }
        mViewModel.getBreedingPairLst(isRefresh, query, mPigeonId)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.editBreedingPairNumResult.observe(this) {
            onRefresh(true)
        }
        /** 删除作育配对 */
        mViewModel.delBreedingPairInfoResult.observe(this) {
            onRefresh(true)
        }
        /** 获取作育配对列表 */
        mViewModel.getBreedingPairLstResult.observe(this) {
            loadListData(it, mAdapter, loadSir, mDatabind.includeList.swipeRecyclerView, mDatabind.includeList.swipeRefreshLayout)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.ivBackSearch,
            mDatabind.btnStartPair,
            mDatabind.includeSearch.tvSearch,
            mDatabind.includeSearch.ivClose,
            mDatabind.includeBar.ivBack
        ) {
            when (it) {
                mDatabind.includeBar.ivBack -> finish()
                mDatabind.btnStartPair -> startActivity<ActivityBloodPigeonPairing>("type" to intentType, "sex" to mSex, "pigeonId" to mPigeonId)
                mDatabind.ivBackSearch -> finish()
                mDatabind.includeSearch.tvSearch -> onRefresh(true)
                mDatabind.includeSearch.ivClose -> {
                    mDatabind.includeSearch.editQuery.setText("")
                    onRefresh(true)
                }
            }
        }
    }
}